<?php

class DefaultController extends Controller {

    public function actionIndex() {
        $c = new CDbCriteria();
        $c->order = 'evak_added desc';
        //$c->compare('evak_user', Yii::app()->user->id);
        $models = Evaks::model()->findAll($c);
        if (isset($_POST['ids'])) {
            $ids = $_POST['ids'];
            foreach ((array) $ids as $id)
                $this->loadModel($id)->delete();
            $this->refresh();
        }
        $this->render('index', array(
            'models' => $models
        ));
    }

    public function actionModerate($id) {
        $model = Evaks::model()->findByPk($id);
        if (!empty($_POST)) {
            $params = array('name' => 'Имя', 'city' => 'Город', 'phone' => 'Телефон', 'mark' => 'Марка', 'type' => 'Тип', 'max' => 'Грузоподъемность', 'option_1' => 'Стоимост', 'option_2' => 'Стоимост', 'option_3' => 'Стоимост', 'option_4' => 'Стоимост', 'option_5' => 'Стоимост', 'option_6' => 'Стоимост', 'option_6' => 'Стоимост');
            $message = '';
            foreach ((array)$_POST['params'] as $paramName => $paramValue) {
                if ($paramValue == 0)
                    $message .= $params[$paramName] . ': ' . $_POST['messages'][$paramName] . '<br />';
            }
            if (!empty($message)) {
                $news = new News();
                $news->news_title = 'несоответствие';
                $news->news_description = $message;
                $news->news_created = time();
                $news->news_user_id = $_POST['user_id'];
                $news->save(false);
                $model->evak_status = 5;
            } else
                $model->evak_status = 1;
            $model->save(false);
            $this->redirect(array('index'));
        }
        $this->render('moderate', array(
            'model' => $model
        ));
    }

    public function loadModel($id) {
        $model = Evaks::model()->findByPk($id);
        if ($model === null) {
            throw new CHttpException(404, 'The requested page does not exist.');
        }
        return $model;
    }

}
